﻿using Core.Shop.Model;
using Core.Shop.Model.DTO;
using Core.Shop.Tool;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static Core.Shop.Filter.AuthAttribute;

namespace Core.Shop.Api
{
    public class LoginController : BaseController
    {
        private PasswordHelper _passwordHelper;
        public LoginController(PasswordHelper passwordHelper)
        {
            _passwordHelper = passwordHelper;
        }
        [NoPermissionRequired]
        [HttpPost]
        public object Post([FromBody]LoginDTO request)
        {
            var password = _passwordHelper.CodePassword(request.Password);
            var user = Db.User.FirstOrDefault(m => m.UserName == request.UserName && m.Password == password);
            if (user == null)
            {
                return ReturnModel.Fail("账号或密码不正确!");
            }
            var tokenPre = "Token:";
            var token = Guid.NewGuid().ToString("N");
            //设置一个五分钟的Token时常
            Redis.SetStringKey(tokenPre + token, user.Id, new TimeSpan(0, 5, 0));

            return ReturnModel.Success("登陆成功!", token);
        }
    }
}
